<?php

namespace App\Repository\Order;

use App\Models\Order\OrderHistoryModel as model;
use App\Repository\BaseRepository;

class HistoryOrderRepository extends  BaseRepository
{
  public function __construct()
  {
    $this->model = new model();
    parent::__construct();
  }
  //列表
  public function index($where)
  {
    $cron = $where;
    $cron['order'] = ['orderby' => 'id', 'orderdir' => 'desc'];
    // $cron['where'] = $where;
    // var_dump($cron,111111111);
    // var_dump($cron, 444444444);
    $data = $this->search($this->model, $cron);
    return $data;
  }
  //添加  
  public function add($data)
  {
    //必填 字段
    $mustField = [
      'order_num',
    ];
    $optionalFields = $this->model->getTableField();
    $msgField = [
      'order_num' => '订单号不能为空',
    ];
    $insertData  = $this->getRealData($data, $mustField, $optionalFields, $msgField);
    if (!empty($insertData) && is_string($insertData)) {
      //返回错误参数
      return $this->returnMsg(
        2,
        $insertData
      );
    }
    $insertData['shop_id'] = !empty($data['shop_id']) ? $data['shop_id'] : (!empty($data->user->shop_id) ? $data->user->shop_id : '');
    $insertData['created_at'] = date('Y-m-d H:i:s'); // 导入时间
    $res = $this->model->insertGetId($insertData);
    if ($res) {
      return $this->returnMsg(200, '设置成功');
    }
    return $this->returnMsg(1, '上传失败');
  }

  //删除
  public function del($where)
  {
    $flag = true;
    $msg = '未知错误';
    $code = 1;
    if (empty($where)) {
      $flag = false;
      $msg = '删除条件不能为空';
      $code = 2;
    }
    if ($flag) {
      $res = $this->model->where($where)->get();
      if (!$res) {
        $flag = false;
        $msg = '未查询到数据';
        $code = 3;
      }
    }
    if ($flag) {
      $del = $this->model->where($where)->delete();
      if ($del) {
        $msg = '删除成功';
        $code = 200;
      }
    }
    return $this->returnMsg($code, $msg);
  }
  //修改 用户修改
  public function edit($where, $data, $isMust = false)
  {
    if (empty($where)) {
      return $this->returnMsg(1, '条件不能为空');
    }
    if (empty($data)) {
      return $this->returnMsg(2, '修改参数不能为空');
    }
    //过滤字段
    //过滤字段
    $optionalFields = $this->model->getTableField();
    $field  = $this->getRealData($data, [], $optionalFields, []);
    //先查询是否存在
    $isTrue = $this->model->where($where)->first();
    if (!$isTrue) {
      return $this->returnMsg(3, '您无权限修改订单信息');
    }
    $str = ''; // 将某某数据修改成了 某某数据
    $isSubmit = false;
    $isEditData = ''; // 未修改
    $isedit = false;
    if (!empty($field['pay_identity'])) {
      $str = '用户修改';
      $isEditData .= '1+'; // 用户信息修改
    }
    if (!empty($field['goods_id'])) {
      $str = '商品信息修改';
      $isSubmit = true; // 商品信息修改
      $isEditData .= '2+';
    }
    if (!empty($field['product_id'])) {
      $str = '产品修改'; // 产品修改
      $isSubmit = true;
      $isEditData .= '3+';
    }
    if (!empty($field['goods_id'])) {
      // $resGoods = $this->goodsModel->where(['id' => $field['goods_id']])->first();
      $resGoods = $this->goodsModel->getOne(['id' => $field['goods_id']]);
      if (empty($resGoods)) {
        return $this->returnMsg(106, '未查询到商品');
      }
      $field['goods_id'] = $resGoods->id;
      $field['goods_code'] = $resGoods->goods_code;
      $field['goods_name'] = $resGoods->name;
      $field['product_id'] = $resGoods->product_id;
      $field['product_name'] = $resGoods->product_name;
      $field['product_code'] = $resGoods->product_code;
    }
    foreach ($field as $key => $v) {
      if ($key == 'goods_id' || $key == 'product_id') {
        continue;
      }
      if ($isTrue->$key != $v) {
        $isedit = true;
        $str .= '将' . $isTrue->$key . '修改为' . $v;
      }
    }
    if (!$isedit) {
      return $this->returnMsg(200, '无修改任何数据');
    }
    // 添加操作日志
    $logDdata = [
      'orderno' => $field['orderno'],
      'remark' => date('Y-m-d H:i:s') . $str,
    ];
    $field['is_edit'] = strpos($resGoods->is_edit, $isEditData) ? $resGoods->is_edit : ($resGoods->is_edit . $isEditData);
    $this->logModel->add($logDdata); // 存入操作日志
    $res = $this->model->where($where)->update($field); //修改订单表
    if ($isSubmit) {
      //重新提交产品
    }
    if ($isMust && $res) {
      return $this->returnMsg(200, '修改成功');
    }
    if (!$isMust) {
      return $this->returnMsg(200, '修改成功');
    }
    return $this->returnMsg(3, '请重新修改');
  }
  public function getOne($where, $field = ['*'])
  {
    return $this->model->where($where)->select($field)->first();
  }
  // 历史订单处理
  public function getManyOrder($field_name = '')
  {
  }
}
